package cn.e4j.modular.api.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.WXUserService;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.service.SysParamsService;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 *功能描述
 * @author caijy
 * @description
 * @datetime 2019-10-8 0008 12:00
 * @return
 */
@RestController
@RequestMapping("api/wx/user")
public class RegisterController extends ControllerSupport {
    private static Logger logger = LoggerFactory.getLogger(RegisterController.class);

    @Autowired
    WXUserService wxUserService;

    @Autowired
    GwaService gwaService;

    @Autowired
    private SystemServiceImpl systemService;

    @Autowired
    private SysParamsService paramsService;

    /**
     * 审核
     *
     * @return
     */
    @RequestMapping(value = "checkUserData")
    public ResponseData checkUserData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        try {

            //匹配 微信账号是否在 后台是否已经审核
            Dto param = new Dto();
            param.put("GWAA001", inDto.getString("GWAA001"));
            param.put("GWAAACTI", "Y");
            GwaaT user = wxUserService.getUserData(param);
            if (ToolUtil.isNotEmpty(user)) {
                return ERROR(600,"此用户已被审核过了!");
            } else {
                SysUser sysUser = new SysUser();
                sysUser.setID(currentUser.getID());
                sysUser.setORG_ID(inDto.getString("GWAA008"));
                sysUser.setTYPE(inDto.getString("GWAA007"));
                sysUser.setDBC(inDto.getString("ENT"));
                //sysUser.setName_(userName);
                //sysUser.setMobile_(mobile);
                param.put("GWAAACTI_Old", "N");
                param.put("GWAAACTI", "Y");
                param.put("GWAA007", inDto.getString("GWAA007"));
                param.put("GWAA008", inDto.getString("GWAA008"));
                param.put("GWAAENT", inDto.getString("ENT"));
                param.put("GWAAMODU", currentUser.getID());
                if (param.getString("GWAA007").equals("1")) {

                    param.put("GWAA009", inDto.getString("GWAA009"));//交易对象编号
                    param.put("GWAA011", inDto.getString("GWAA011"));
                    param.put("GWAA013", inDto.getString("GWAA013"));
                    param.put("GWAA017", inDto.getString("GWAA017"));
                    param.put("GWAA018", inDto.getString("GWAA018"));

                    sysUser.setEMAIL(param.getString("GWAA011"));
                    sysUser.setADDRESS(param.getString("GWAA13"));
                }
                SysUserRole sys_user_rolePO = new SysUserRole();
                sys_user_rolePO.setID(DateUtils.getDate("yyyyMMddHHmmssSSS"));
                sys_user_rolePO.setCREATE_TIME(LocalDateTime.now());
                sys_user_rolePO.setCREATE_BY(currentUser.getID());
                sys_user_rolePO.setUSER_ID(currentUser.getID());
                sys_user_rolePO.setROLE_ID(inDto.getString("ROLE_ID"));

                wxUserService.checkUserSave(param, sysUser, sys_user_rolePO);

                WeixinHandler.sendMsg(param,
                        String.format("恭喜您的申请已经审核通过！审核员[%s],审核时间[%s]",
                        currentUser.getNAME(),
                        DateUtils.getDate(DateUtils.PATTERN_yyyy_MM_dd_HH_mm_ss)));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ERROR("审核异常!");
        }

        return SUCCESS;
    }

    @RequestMapping(value = "getGwadDatas")
    public ResponseData getGwadDatas(@DtoParam DtoWapper wapper) {
        Dto inDto =wapper.getDto();
        inDto.put("LANG", "2");
        List<Dto> list = gwaService.getGwad(inDto);
        return SUCCESS(list);
    }

    //客户行业 一级分类
    @RequestMapping(value = "getGwbwDatas")
    public ResponseData getGwbwDatas(@DtoParam DtoWapper wapper) {
        Dto inDto =wapper.getDto();
        inDto.put("LANG", "2");
        return SUCCESS(gwaService.getGwbw(inDto));
    }

    //客户行业 二级分类
    @RequestMapping(value = "getGwagDatas")
    public ResponseData getGwagDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("LANG", "2");
        return SUCCESS(gwaService.getGwag(inDto));
    }


    @RequestMapping(value = "getPlantDatas")
    public ResponseData getPlantDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            list = gwaService.getOrg(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    //客户名称
    @RequestMapping(value = "getGwakDatas")
    public ResponseData getGwakDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        inDto.put("LANG", "2");
        try {
            list = gwaService.getGwak(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    @ResponseBody
    @RequestMapping(value = "getProvinces")
    public ResponseData getProvinces(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("LANG", "2");
        return SUCCESS(gwaService.getProvinces(inDto));
    }

    @ResponseBody
    @RequestMapping(value = "getCitys")
    public ResponseData getCitys(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("LANG", "2");
        List<Dto> list = gwaService.getCitys(inDto);
        return SUCCESS(list);
    }

    @ResponseBody
    @RequestMapping(value = "getDistricts")
    public ResponseData getDistricts(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("LANG", "2");
        List<Dto> list = gwaService.getDistricts(inDto);
        return SUCCESS(list);
    }

}
